package com.wn.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.common.pojo.PageParam;
import com.wn.common.pojo.Rsp;
import com.wn.common.pojo.TravelAngency;
import com.wn.mapper.TravelAngencyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;


@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class TravelAngncyService {


    @Autowired
    private TravelAngencyMapper travelAngencyMapper;



    //查找旅行社
    public PageInfo<TravelAngency> queryAll(PageParam<TravelAngency> pageParam){

        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //分页操作
            PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
            List<TravelAngency> travelAngencies = travelAngencyMapper.superSearch(pageParam.getSuperSearchKeyWord());

            PageInfo<TravelAngency> travelAngencyPageInfo = new PageInfo<TravelAngency>(travelAngencies);
            return travelAngencyPageInfo;

        }else {
            //分页操作
            PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
            List<TravelAngency> travelAngencyList = travelAngencyMapper.selectAll();
            PageInfo<TravelAngency> travelAngencyPageInfo = new PageInfo<TravelAngency>(travelAngencyList);
            return travelAngencyPageInfo;
        }
    }

    //增加旅行社
    public Rsp addtravel(TravelAngency travelAngency){

        return travelAngencyMapper.insertSelective(travelAngency) ==1?new Rsp().success(0):new Rsp().fail();
    }

    //根据id查询
    public Rsp getTravelById(Integer travelId){
        TravelAngency travelAngency = travelAngencyMapper.selectByPrimaryKey(travelId);
        if(StringUtils.isEmpty(travelAngency)){
            return new Rsp().fail();
        }
        return new Rsp().success(travelAngency);
    }

    //根据id进行逻辑删除
    public Rsp deleteByPrimaryKey(TravelAngency travelAngency){
        int primaryKey = travelAngencyMapper.deleteByPrimaryKey(travelAngency.getTravelId());
        if(primaryKey!=0){
            return new Rsp().success(primaryKey);
        }else {
            return new Rsp().fail().msg("删除失败");
        }
    }

    //根据id修改
    public Rsp updateByPrimaryKey(TravelAngency travelAngency,Integer updateUserId){
        //获取当前时间
        Date nowdate = new Date();
        //转换时间格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        travelAngency.setUpdateUserId(updateUserId);
        travelAngency.setUpdateTime(Timestamp.valueOf(simpleDateFormat.format(nowdate)));
//        System.out.println(travelAngencyMapper.updateByPrimaryKeySelective(travelAngency));
        return travelAngencyMapper.updateByPrimaryKeySelective(travelAngency)==1?
                new Rsp().success(null) : new Rsp().fail();

    }

    //
    public Rsp updateNewsinfo(TravelAngency travelAngency) {

        if(travelAngency.getStatus().equals("1")){
            travelAngency.setStatus("0");
        }else{
            travelAngency.setStatus("1");
        }
        int i = travelAngencyMapper.updateNewsinfo(travelAngency.getTravelId(), travelAngency.getStatus());

        if(i == 1){
            return Rsp.success(null);
        }else{
            return Rsp.fail();
        }
    }
}
